package com.company.observer.v07;

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        Child child = new Child();
//        while (!child.isCry()){
//            try {
//                Thread.sleep(1000);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//            System.out.println("observer........");
//        }
        child.wakeUp();
    }
}

abstract class Event<T> {
    abstract T getSource();
}

class WakeUpEvent extends Event<Child> {
    long timestamp;
    String loc;
    Child source;//原对象

    public WakeUpEvent(long timestamp, String loc, Child source) {
        this.timestamp = timestamp;
        this.loc = loc;
        this.source = source;
    }

    @Override
    Child getSource() {
        return source;
    }
}

class Dad implements Observer {
    public void feed() {
        System.out.println("Dad feeding.............");
    }

    @Override
    public void actionWakeUp(WakeUpEvent wup) {
        feed();
    }
}

class Mum implements Observer {

    @Override
    public void actionWakeUp(WakeUpEvent wup) {
        hug();
    }

    public void hug() {
        System.out.println("mum.hug........");
    }
}

class Dog implements Observer {

    @Override
    public void actionWakeUp(WakeUpEvent wup) {
        Talk();
    }

    public void Talk() {
        System.out.println("........");
    }
}

interface Observer {
    void actionWakeUp(WakeUpEvent wup);
}


class Child {
    private boolean cry = false;
    List<Observer> list = new ArrayList<>();

    {
        list.add(new Dad());
        list.add(new Mum());
        list.add(new Dog());
    }

    public boolean isCry() {
        return cry;
    }

    public void wakeUp() {
        cry = true;
        WakeUpEvent wup = new WakeUpEvent(System.currentTimeMillis(), "bed", this);
        System.out.println("this=" + this);
        for (Observer observer :
                list) {
            observer.actionWakeUp(wup);
        }
    }

    @Override
    public String toString() {
        return "Child{" +
                "cry=" + cry +
                ", list=" + list +
                '}';
    }
}